package com.wxd.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxd.reggie.common.R;
import com.wxd.reggie.dto.DishDto;
import com.wxd.reggie.entity.Category;
import com.wxd.reggie.entity.Dish;
import com.wxd.reggie.entity.DishFlavor;
import com.wxd.reggie.service.CategoryService;
import com.wxd.reggie.service.DishFlavorService;
import com.wxd.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增菜品
     *
     * @param dishDto
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto) {
        dishService.saveDishOfFlave(dishDto);
        return R.success("添加菜品成功");
    }

    /**
     * 获取菜品分页数据
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        // 分页参数
        Page<Dish> pageInfo = new Page(page, pageSize);
        Page<DishDto> info = new Page();
        // 查询参数
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper();
        lqw.like(name != null, Dish::getName, name);
        // 根据更新时间降序排列
        lqw.orderByDesc(Dish::getUpdateTime);
        dishService.page(pageInfo, lqw);
        // 对象拷贝
        BeanUtils.copyProperties(pageInfo, info, "records");
        List<Dish> records = pageInfo.getRecords();
        // 循环查询数据进行赋值
        List<DishDto> list = records.stream().map(item -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item, dishDto);
            Long categoryId = item.getCategoryId();
            Category categoryItem = categoryService.getById(categoryId);
            if (categoryItem != null) {
                dishDto.setCategoryName(categoryItem.getName());
            }
            return dishDto;
        }).collect(Collectors.toList());
        info.setRecords(list);
        return R.success(info);
    }

    /**
     * 获取单个菜品数据
     *
     * @param id
     */
    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable long id) {
        DishDto dishDto = dishService.getDishOfFlavorById(id);
        return R.success(dishDto);
    }

    /**
     * 修改菜品
     *
     * @param dishDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto) {
        dishService.updateDish(dishDto);
        return R.success("修改成功");
    }

    /**
     * 根据categoryId获取分类下的菜品 参数可能不止一个 所以用实体类封装会更好
     *
     * @param dish
     * @return
     */
    @GetMapping("/list")
    public R<List<DishDto>> getDishByCategoryId(Dish dish) {
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId());
        lqw.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        List<Dish> list = dishService.list(lqw);
        List<DishDto> dishDtoList = list.stream().map(item -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item, dishDto);
            Long dishId = item.getId();
            LambdaQueryWrapper<DishFlavor> lqwDto = new LambdaQueryWrapper<>();
            lqwDto.eq(DishFlavor::getDishId, dishId);
            List<DishFlavor> list1 = dishFlavorService.list(lqwDto);
            dishDto.setFlavors(list1);
            return dishDto;
        }).collect(Collectors.toList());
        return R.success(dishDtoList);
    }

    /**
     * 修改/批量修改状态
     *
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable int status, String ids) {
        if (ids == null || ids.isEmpty()) {
            return R.error("ID列表不能为空");
        }
        // 将字符串ids转换为整数列表
        String[] idArray = ids.split(",");
        List<Long> idList = Arrays.stream(idArray).map(Long::valueOf).collect(Collectors.toList());
        LambdaUpdateWrapper<Dish> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Dish::getStatus, status).in(Dish::getId, idList);
        boolean update1 = dishService.update(updateWrapper);
        log.info("更新结果{}", update1);
        return R.success("更新成功");
    }

    /**
     * 删除/批量删除数据
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(String ids) {
        if (ids == null || ids.isEmpty()) {
            return R.error("ids列表不能为空");
        }
        String[] idArray = ids.split(",");
        List<Long> idList = Arrays.stream(idArray).map(Long::valueOf).collect(Collectors.toList());
        LambdaQueryWrapper<Dish> updateWrapper = new LambdaQueryWrapper<>();
        updateWrapper.in(Dish::getId, idList);
        boolean remove = dishService.remove(updateWrapper);
        log.info("更新结果{}", remove);
        return R.success("删除成功");
    }
}
